package com.hotel.hotelsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.hotelsystem.mapper.RoomInfoMapper;
import com.hotel.hotelsystem.mapper.RoomListMapper;
import com.hotel.hotelsystem.pojo.Room;
import com.hotel.hotelsystem.pojo.RoomInfo;
import com.hotel.hotelsystem.pojo.RoomList;
import com.hotel.hotelsystem.service.RoomService;
import com.hotel.hotelsystem.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author magtop
* @description 针对表【room】的数据库操作Service实现
* @createDate 2022-10-21 17:51:30
*/
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{
    @Autowired
    private RoomListMapper roomListMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public Map<Integer,Integer> getAllTypeRoomNumByTime(int hotelId, LocalDateTime inTime, LocalDateTime outTime) {
        Map<Integer,Integer> numOfType = new HashMap<>();
        QueryWrapper<RoomList> wq0 = new QueryWrapper<>();
        wq0.eq("hotel_id",hotelId);
        List<RoomList> roomList = roomListMapper.selectList(wq0);
        for(RoomList roomType:roomList){
            QueryWrapper<Room> qw = new QueryWrapper<>();
            qw.eq("room_status",0);
            qw.eq("room_type_id",roomType.getRoomTypeId());
            qw.eq("hotel_id",hotelId);
            List<Room> rooms = roomMapper.selectList(qw);   //查询房间类型相符的房间
            System.out.println("***rooms: "+rooms);
            List<Room> fitRooms = new ArrayList<>();
            for(Room room:rooms){
                int roomNum = room.getRoomNum();
                System.out.println("***roomNum: "+roomNum);
                QueryWrapper<RoomInfo> qw1 = new QueryWrapper<>();
                qw1.eq("room_num",roomNum);
                qw1.and(wrapper->
                        wrapper.and(wrapper1->
                                        wrapper1.lt("in_time",inTime).gt("in_time",outTime))
                                .or(wrapper2->
                                        wrapper2.lt("out_time",inTime).gt("out_time",outTime))
                                .or(wrapper3->
                                        wrapper3.eq("in_time",inTime).eq("out_time",outTime)));

                List<RoomInfo> roomInfoList = roomInfoMapper.selectList(qw1);//查询该房间号在用户住房期间是否被占用
                System.out.println("****roomInfoList: "+roomInfoList);
                if(roomInfoList.size()==0){
                    fitRooms.add(room);
                }
            }
            numOfType.put(roomType.getRoomTypeId(),fitRooms.size());
        }
        return numOfType;
    }
}




